<?php

namespace app\admin\controller;

use think\Db;

class User extends Authorized
{

    public function index(){return $this->fetch();}
    public function qf(){

        Db::table('tb_user')->where(['state'=>1,'money'=>['<',0]])->setField(['qftime'=>date('Y-m-d H:i:s',time())]);
        Db::table('tb_webinfo w')->join('tb_user u','w.userid=u.id','left')
            ->where(['u.state'=>1, 'u.money'=>['<',0], 'w.state'=>1, 'w.qftime'=>null,])->setField(['qftime'=>date('Y-m-d H:i:s',time())]);
        return $this->fetch();
    }
    public function stop(){return $this->fetch();}
    public function state(){return $this->fetch();}


    public function getList()
    {
        $get_data=$this->request->get();
        $post_data = $this->request->post();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $orderType = input('order');  //排序
        $keywords = input('keywords');
        $usertype=input('usertype');
        $state = input('state');
        $where=[];
        $order='';

        if (!empty($orderType)){
            if ($orderType=='money_asc'){$order='money';}
            if ($orderType=='money_desc'){$order='money desc';}
            if ($orderType=='moneypay_asc'){$order='moneypay';}
            if ($orderType=='moneypay_desc'){$order='moneypay desc';}
            if ($orderType=='closetime_asc'){$order='closetime asc';}
            if ($orderType=='closetime_desc'){$order='closetime desc';}
        }else{
            $order='state,id desc';
        }

        if (!empty($usertype)){

            if ($usertype=='state'){   $where['state']=0;}
            if ($usertype=='zc'){
                $where['state']=1;
                $where['qftime']=null;
            } //正常客户
            if ($usertype=='qf'){
                $where['state']=1;
                $where['money']=['<',0];
            } //欠费未丢单客户
            if ($usertype=='stop'){
                $where['state']=3;
            }//丢单客户

        }
        if (!empty($keywords)){
            $where['name']=['like','%'.$keywords.'%'];
        }




        $count=Db::table('tb_user')->where($where)->count();
        $rows=Db::table('tb_user')->where($where)->order($order)->page($page, $limit)->select();
        return json(['code' =>0,'count' =>$count,'msg' =>'','data'=>$rows]);
    }

    public function add()
    {
        $post_data = $this->request->post();
        if($post_data){
            Db::table('tb_user')->startTrans();
            $id =Db::table('tb_user')->insertGetId($post_data);
            if (false === $id) {
                Db::table('tb_user')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'添加失败','data'=>'']);
            } else {
               Db::table('tb_user')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'添加成功','data'=>'']);
            }
        }

       return $this->fetch();
    }

    public function edit()
    {
        $post_data = $this->request->post();
        if($post_data){
            $id=array_shift($post_data);
            $result =Db::table('tb_user')->where('id',$id)->setField($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }


        $article=Db::table('tb_user')->where('id' , input('id'))->find();
        $this->assign('article', $article);
       return $this->fetch();
    }


    public function del()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('tb_user')->where('id', $post_data['id'])->delete();
            if (false === $result) {
                return json(['msg' =>'删除失败', 'code' =>1]);
            } else {
                return json(['msg' =>'删除成功', 'code' =>0]);
            }
        }
    }

    public function stop_user()
    {
        $post_data = $this->request->post();
        if($post_data){
            Db::table('tb_user')->startTrans();
            $result =Db::table('tb_user')->where('id', $post_data['id'])->setField([
                'state'=>3,
                'webNum'=>0,
                'keyNum'=>0,
                'closetime'=>date('Y-m-d H:i:s',time())
            ]);

            if (false === $result) {
                Db::table('tb_user')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'停用失败','data'=>'']);
            } else {
                Db::table('tb_user')->commit();
                Db::table('tb_webinfo')->where('userid',$post_data['id'])->setField(['state'=>3,'keyNum'=>0]);
                Db::table('tb_webkey')->where('userid',$post_data['id'])->setField(['state'=>3]);
                return json(['code' =>0,'count' =>0,'msg' =>'停用成功','data'=>'']);
            }
        }
    }

    public function reup_user()
    {
        $post_data = $this->request->post();
        if($post_data){
            Db::table('tb_user')->startTrans();
            $result =Db::table('tb_user')->where('id', $post_data['id'])->setField(['state'=>1,'closetime'=>null]);

            if (false === $result) {
                Db::table('tb_user')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'启用失败','data'=>'']);
            } else {
                Db::table('tb_user')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'启用成功','data'=>'']);
            }
        }
    }
}